#!/usr/bin/env python
# -*- coding:utf-8 -*-

class calc1():
    def __init__(self, gw, gh):
        self.gw = gw
        self.gh = gh
        self.sk = "N/A"
        self.jc = "N/A"
        self.tt = "N/A"
        self.gw_err = 0
        self.gh_err = 0
        self.gw_range_list = (32, 48, 72, 110, 159, 500)
        self.gh_range_list = (("4",8,15),["6",10,16,24,36,40,"50"],("6",25,40))
        g1 = ("GS-1、（CSK-IA）", "Φ2×20-24dB", "5P 6×6 K2.5")
        g2 = ("GS-1、（CSK-IA）", "Φ2×20-20dB", "5P 6×6 K2.5")
        g3 = ("GS-1、（CSK-IA）", "Φ2×20-16dB", "2.5P 6×6 K2")
        g4 = ("GS-2、（CSK-IA）", "Φ2×20-24dB", "5P 6×6 K2.5")
        g5 = ("GS-2、（CSK-IA）", "Φ2×20-20dB", "5P 6×6 K2.5")
        g6 = ("GS-2、（CSK-IA）", "Φ2×20-16dB", "2.5P 6×6 K2")
        g7 = ("GS-3、（CSK-IA）", "Φ2×20-24dB", "5P 6×6 K2.5")
        g8 = ("GS-3、（CSK-IA）", "Φ2×20-20dB", "5P 6×6 K2.5")
        g9 = ("GS-3、（CSK-IA）", "Φ2×20-16dB", "2.5P 6×6 K2")
        g10 = ("GS-4、（CSK-IA）", "Φ2×20-24dB", "5P 6×6 K2.5")
        g11 = ("GS-4、（CSK-IA）", "Φ2×20-20dB", "5P 6×6 K2.5")
        g12 = ("GS-4、（CSK-IA）", "Φ2×20-16dB", "2.5P 6×6 K2")
        g13 = ("CSK-ⅠA、RB-C-1、（CSK-ⅡA）", "Φ2×40-18dB", "2.5（5）P 6×6 K2（2.5）或2.5（5）P 9×9 K2（2.5）")
        g14 = ("CSK-ⅠA、RB-C-2、（CSK-ⅡA）", "Φ2×40-18dB", "2.5（5）P 6×6 K2（2.5）或2.5（5）P 9×9 K2（2.5）")
        g15 = ("CSK-ⅠA、RB-C-3、（CSK-ⅡA）", "Φ2×40-18dB", "2.5（5）P 6×6 K2（2.5）或2.5（5）P 9×9 K2（2.5）")
        g16 = ("CSK-ⅠA、RB-C-4、（CSK-ⅡA）", "Φ2×40-18dB", "2.5（5）P 6×6 K2（2.5）或2.5（5）P 9×9 K2（2.5）")
        g17 = ("CSK-ⅠA、RB-C-5、（CSK-ⅡA）", "Φ2×40-18dB", "2.5（5）P 6×6 K2（2.5）或2.5（5）P 9×9 K2（2.5）")
        g18 = ("CSK-ⅠA、RB-C-5、（CSK-ⅡA）", "Φ2×60-14dB", "2.5P 6×6 K1、K2或2.5 P 9×9 K1、K2（须两种K值探头）")
        g19 = ("CSK-ⅠA、CSK-ⅡA", "Φ2×40-18dB", "2.5（5）P 9×9 K2（2.5）")
        g20 = ("CSK-ⅠA、CSK-ⅡA", "Φ2×60-14dB", "2.5 P 9×9 K1、K2（须两种K值探头）")
        self.group_map = (
        (g1, g2, g3), (g4, g5, g6), (g7, g8, g9), (g10, g11, g12), (g13, g14, g15, g16, g17, g18), (g19, g19, g20))
        # self.gw_map = (("4",8,15),("6",10,16,24,36,40,50),("6",25,40))

    def gw_calc(self):
        # range_list = (32, 48, 72, 110, 159, 500)

        if self.gw < min(self.gw_range_list):
            self.gw_err = 1
        for i, j in enumerate(self.gw_range_list):
            if self.gw >= j and self.gw < self.gw_range_list[i + 1]:
                self.group_cat = i  # start from 0 return the group of gw
        return None

    def gh_calc(self):
        if self.group_cat < 4:
            self.gh_range = self.gh_range_list[0]
        elif self.group_cat == 4:
            self.gh_range = self.gh_range_list[1]
        else:
            self.gh_range = self.gh_range_list[2]  # ("6",10,16,24,36,40)

        if self.gh < float(self.gh_range[0]):
            self.gh_err = 1
            return None
        if type(self.gh_range[-1]) == str:
            if self.gh > float(self.gh_range[-1]):
                self.gh_err = 1
                return None
            else:
                del self.gh_range[-1]  # ("6",10,16,24,36,40)
                self.gh_range = self.gh_range

        for i, ran in enumerate(self.gh_range):
            if type(ran) == str:
                if self.gh >= float(ran) and self.gh <= self.gh_range[i+1]:  # 0, 6, i=0
                    return i
            else:
                if self.gh > self.gh_range[-1]:
                    i = len(self.gh_range)-1
                    return i
                if (self.gh > ran and self.gh <= self.gh_range[i+1]):
                    return i

    def calc(self):
        self.gw_calc()
        if not self.gw_err:
            gh_rsl = self.gh_calc()
            if gh_rsl and gh_rsl >= 0 :
                group_1 = self.group_map[self.group_cat]
                fnl = group_1[gh_rsl]  # ("CSK-ⅠA、RB-C-5、（CSK-ⅡA）", "Φ2×40-18dB", "2.5（5）P 6×6 K2（2.5）或2.5（5）P 9×9 K2（2.5）")
                self.sk = fnl[0]
                self.jc = fnl[1]
                self.tt = fnl[2]
        return (self.gw_err, self.gh_err, self.sk, self.jc, self.tt)

    # def ttj_value(self):
    #     gw_er = 0
    #     gh_er = 0
    #     if 32 <= self.gw < 159:
    #         if 4 <= self.gh <= 8:
    #             self.ttk = "2.5-3.0"
    #             self.ttf = "4.0-5.0"
    #             self.jc = "Φ2×20-24dB"
    #         elif 8 < self.gh  <= 15:
    #             self.ttk = "2.0-2.5"
    #             self.ttf = "4.0-5.0"
    #             self.jc = "Φ2×20-20dB"
    #         elif self.gh  > 15:
    #             self.ttk = "1.5-2.0"
    #             self.ttf = "2.0-2.5"
    #             self.jc = "Φ2×20-16dB"
    #         else:
    #             gh_er = 1
    #             # self.ttk = "N/A"
    #             # self.ttf = "N/A"
    #             # self.jc = "N/A"
    #     elif 159 <= self.gw <500:
    #         if 6 <= self.gh <= 25:
    #             self.ttk = "2.0-3.0"
    #             self.ttf = "2.0-5.0"
    #             self.jc = "Φ2×40-18dB"
    #         elif 25 < self.gh <= 40:
    #             self.ttk = "1.5-2.5"
    #             self.ttf = "2.0-5.0"
    #             self.jc = "Φ2×40-18dB"
    #         elif self.gh > 40:
    #             self.ttk = "1.0-2.0"
    #             self.ttf = "2.0-2.5"
    #             self.jc = "Φ2×60-14dB"
    #         else:
    #             gh_er = 1
    #             # self.ttk = "N/A"
    #             # self.ttf = "N/A"
    #             # self.jc = "N/A"
    #     elif self.gw >= 500:
    #         if 6 <= self.gh <= 25:
    #             self.ttk = "2.0-3.0"
    #             self.ttf = "4.0-5.0"
    #             self.jc = "Φ2×40-18dB"
    #         elif 25 < self.gh <= 40:
    #             self.ttk = "1.5-2.5"
    #             self.ttf = "2.0-5.0"
    #             self.jc = "Φ2×40-18dB"
    #         elif self.gh > 40:
    #             self.ttk = "1.0-2.0"
    #             self.ttf = "2.0-2.5"
    #             self.jc = "Φ2×60-14dB"
    #         else:
    #             gh_er = 1
    #             # self.ttk = "N/A"
    #             # self.ttf = "N/A"
    #             # self.jc = "N/A"
    #     else:
    #         gw_er = 1
    #         # self.ttk = "N/A"
    #         # self.ttf = "N/A"
    #         # self.jc = "N/A"
    #     return (gw_er, gh_er)
    #
    # def gh_calc(self,input_val, range_list, val_group):
    #     """
    #     用于计算输入数值对应的类型
    #     :param input_val: float,
    #     :param range_list: a tuple or list, ((e4,8),(8,15) )
    #     :param val_group: a tuple or list
    #     :return:
    #     """
    #
    #     for i, ran in enumerate(range_list):
    #         low_b = ran[0]
    #         up_b = ran[1]
    #         if type(low_b) == str:
    #             if input_val >= float(low_b) and input_val <= up_b:
    #                 # idx = i
    #                 break
    #         else:
    #             if input_val > float(low_b) and input_val <= up_b:
    #                 # idx = i
    #                 break
    #         idx = i
    #     return val_group[i]
    #
    # def sk_type(self):
    #     # gw = float(self.m_textCtrl2.GetValue())
    #     # gh = float(self.m_textCtrl3.GetValue())
    #     gw_er = 0
    #     gh_er = 0
    #
    #     if 32 <= self.gw < 48:
    #         if 4 <= self.gh <= 8:
    #             self.sk = "GS-1、（CSK-IA）"
    #             self.jc = "Φ2×20-24dB"
    #     else:
    #         gw_er = 1
    #     return (gw_er, gh_er, self.sk, self.jc, self.tt)
    #
    #
    #     if 32 <= self.gw < 159:
    #         if self.gh <= 4: #or self.gh >= 50:
    #             # self.sk = "N/A"
    #             gh_er = 1
    #         else:
    #             if 32 <= self.gw < 48:
    #                 self.sk = "GS-1"
    #             elif 48 <= self.gw < 72:
    #                 self.sk = "GS-2"
    #             elif 72 <= self.gw < 110:
    #                 self.sk = "GS-3"
    #             else:
    #                 self.sk = "GS-4"
    #     elif 159 <= self.gw < 500:
    #         if self.gh < 6 or self.gh > 50:
    #             # self.sk = "N/A"
    #             gh_er = 1
    #         else:
    #             if 6 <= self.gh <= 10:
    #                 self.sk = "CSK-ⅠA, RB-C-1"
    #             elif 10 < self.gh <= 16:
    #                 self.sk = "CSK-ⅠA, RB-C-2"
    #             elif 16 < self.gh <= 24:
    #                 self.sk = "CSK-ⅠA, RB-C-3"
    #             elif 24 < self.gh <= 36:
    #                 self.sk = "CSK-ⅠA, RB-C-4"
    #             elif 36 < self.gh <= 50:
    #                 self.sk = "CSK-ⅠA, RB-C-5"
    #     elif self.gw >=500:
    #         if self.gh < 6 or self.gh >150:
    #             # self.sk = "N/A"
    #             gh_er = 1
    #         else:
    #             self.sk = "CSK-ⅠA、CSK-ⅡA"
    #     else:
    #         gw_er = 1
    #         # self.sk = "N/A"
    #     return ( gw_er, gh_er, self.sk)